package com.hsgene.common.util.date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;

/**
 * 时间日期格式解析
 *
 * @author wangbing
 * @version 1.0, 2018/07/10
 */
public class BirthdayDateSerializer extends JsonSerializer<Date> {

    private static final Logger logger = LogManager.getLogger(BirthdayDateSerializer.class);

    @Override
    public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) {
        if (value == null) {
            return;
        }
        try {
            gen.writeString(SimpleDateUtils.formatDate(value, SimpleDateUtils.DatePattern.YYYY_MM_DD));
        } catch (Exception e) {
            logger.error("日期时间JSON格式化异常：{}", value.toString());
            logger.error(e.getMessage(), e);
        }
    }
}
